package com.bocai.kelalacar.ui.driver.main.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.CheckLineBean;
import com.bocai.kelalacar.bean.MyAddressBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.act.CarTeamsActivity;
import com.bocai.kelalacar.ui.driver.main.contract.DriverPublishContract;
import com.bocai.kelalacar.ui.driver.main.module.DriverPublishModule;
import com.bocai.kelalacar.ui.driver.main.presenter.DriverPublishPresenter;
import com.bocai.kelalacar.util.DateUtil;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/28.
 */

public class DriverPublishInforActivity extends BaseActivity implements DriverPublishContract.view{
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_start_data)
    TextView tvStartData;
    @BindView(R.id.ll_start_data)
    LinearLayout llStartData;
    @BindView(R.id.tv_end_data)
    TextView tvEndData;
    @BindView(R.id.ll_end_data)
    LinearLayout llEndData;
    @BindView(R.id.check_agree)
    CheckBox checkAgree;
    @BindView(R.id.btn_publish)
    AppCompatButton btnPublish;
    @Inject
    DriverPublishPresenter presenter;
    CheckLineBean bean;
    TimePickerView pickerView;
    private int index = 0;
    @BindView(R.id.ll_back)
    LinearLayout llBack;

    @Override
    public int getLayoutId() {
        return R.layout.act_driver_publish_infor;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new DriverPublishModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("发布行程");
        initBack(imgBack,llBack);
        bean = (CheckLineBean) getIntent().getSerializableExtra("data");
        setDataView();

    }
    public void setDataView() {
        pickerView = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                if (index == 1) {
                    tvStartData.setText(DateUtil.dateToDateTimeString(date));
                }else if (index == 2){
                    Date date1 = new Date();
                    Log.e("data",date.getTime() - date1.getTime()+"");
                    if (date.getTime() - date1.getTime() <= 6*60*60*1000){
                        showErrorTip("出行时间小于6小时");
                    }else {
                        tvEndData.setText(DateUtil.dateToDateTimeString(date));
                    }
                }

                if (!TextUtils.isEmpty(tvStartData.getText().toString())&&!TextUtils.isEmpty(tvEndData.getText().toString())){
                    btnPublish.setEnabled(true);
                    btnPublish.setTextColor(getResources().getColor(R.color.color_white));
                }else {
                    btnPublish.setEnabled(false);
                    btnPublish.setTextColor(getResources().getColor(R.color.text_color));
                }
            }
        })
                .setType(TimePickerView.Type.YEAR_MONTH_DAY_HOUR_MIN)
                .build();
    }

    @OnClick({R.id.ll_start_data,R.id.ll_end_data,R.id.btn_publish,R.id.tv_cat_teams})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.ll_start_data:
                index = 1;
                pickerView.show();
                break;
            case R.id.ll_end_data:
                index = 2;
                pickerView.show();
                break;
            case R.id.btn_publish:
                if (TextUtils.isEmpty(tvStartData.getText().toString())){
                    showErrorTip("请选择用车开始时间");
                }else if(TextUtils.isEmpty(tvEndData.getText().toString())){
                    showErrorTip("请选择用车开始时间");
                }else if(!checkAgree.isChecked()){
                    showErrorTip("请阅读并同意服务协议");
                }else {
                    presenter.publishStroke(bean.getPriceList().get(0).getPriceId(),tvStartData.getText().toString(),tvEndData.getText().toString());
                }
                break;
            case R.id.tv_cat_teams:
                startActivity(CarTeamsActivity.class);
                break;
        }
    }

    @Override
    public void checkLineSuccess(CheckLineBean bean) {

    }

    @Override
    public void publishStrokeSuccess() {
        showErrorTip("发布成功");
        setResult(11);
        startActivity(MainActivity.class);
        finish();
    }

    @Override
    public void originAreasList(List<MyAddressBean> list) {

    }

    @Override
    public void arriveAreasList(List<MyAddressBean> list) {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }
}
